var ACTION = require("../../base/action");
var common = require("./common");
var utils = require("../../utils/utils");
var ocrGml = require("../../utils/ocrGml");
var nextVideo = require("../appCommon/nextVideo");
var aRead = new ACTION("看视频", {
  storage: common.storage,
});

aRead.exec = function () {
  console.show(true);
  this.readAll((3 + 1) * 3600);
  console.hide();
};
aRead.readAll = function (span) {
  var startTime = Date.now();
  while (true) {
    nextVideo();
    var readSpan = utils.getRandom(25, 120);
    this.read(readSpan);
    var now = Date.now();
    var hasWatch = (now - startTime) / 1000;
    if (hasWatch > span) {
      console.log("已经看了-", hasWatch, "/", span, "-s");
      break;
    }
  }
};
aRead.checkIsOver = function () {
  var contents = ["重播", "立即关注", "点击进入"];
  if (utils.existsOneTextStartsWith(contents)) {
    return true;
  }
  if (ocrGml.captureExistOne(contents, true)) {
    return true;
  }
  return false;
};
aRead.read = function (span) {
  var startTime = Date.now();
  var count = 0;
  console.log("将看视频-" + span + "-s");
  while (true) {
    sleep(20 * 1000);
    var now = Date.now();
    var hasWatch = (now - startTime) / 1000;
    if (hasWatch > span) {
      console.log("已经看了-", hasWatch, "/", span, "-s");
      break;
    }
    if (this.checkIsOver()) {
      console.log("视频已经结束，将看下一个");
      break;
    }
    count++;
    if (count % 5 == 0) {
      console.log("已经看了-", hasWatch, "/", span, "-s");
    }
  }
};
module.exports = aRead;
